package com.loveprogrammer.handler;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.base.network.support.BaseHandler;
import com.loveprogrammer.command.anotation.TagListener;
import com.loveprogrammer.command.anotation.TopicListener;
import com.loveprogrammer.command.client.ClientMenuTag;
import com.loveprogrammer.command.client.ClientTopic;
import com.loveprogrammer.command.server.ServerBattleTag;
import com.loveprogrammer.command.server.ServerTopic;
import com.loveprogrammer.constants.CommonValue;
import com.loveprogrammer.convert.MapTemplateConvert;
import com.loveprogrammer.dto.base.StringRequest;
import com.loveprogrammer.dto.battle.BattleFightResult;
import com.loveprogrammer.dto.battle.FightChoose;
import com.loveprogrammer.dto.battle.MapChoose;
import com.loveprogrammer.engine.battle.BattleEngine;
import com.loveprogrammer.factory.MapFactory;
import com.loveprogrammer.factory.template.MapTemplate;
import com.loveprogrammer.model.Hero;
import com.loveprogrammer.model.Monster;
import com.loveprogrammer.model.SessionAttributeKey;
import com.loveprogrammer.model.battle.TownMap;
import com.loveprogrammer.model.game.HeroWrapper;
import com.loveprogrammer.model.game.UserGameWrapper;
import com.loveprogrammer.pojo.StringMessage;
import com.loveprogrammer.utils.BeanCopyUtils;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName BattleHandler
 * @Description 战斗相关的执行类
 * @Author admin
 * @Date 2024/2/5 15:49
 * @Version 1.0
 */
@Component
@TopicListener(topic = ServerTopic.TOPIC_BATTLE)
public class BattleHandler extends BaseHandler {

    public static final Logger log = LoggerFactory.getLogger(BattleHandler.class);

    @TagListener(tag = ServerBattleTag.TAG_BATTLE_MAPS,messageClass = StringRequest.class)
    public StringMessage mapList(ChannelHandlerContext ctx, StringRequest data) throws Exception {
        List<MapTemplate> mapTemplates = MapFactory.mapTemplates();
        // 获取地图列表
        StringMessage message = StringMessage.create(ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_MAP_CHOOSE);
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);

        MapChoose mapChoose = new MapChoose(message);
        mapChoose.setMapList(MapTemplateConvert.INSTANCE.toMapItems(mapTemplates));

        message.setBody(JSON.toJSONString(mapChoose));
        return message;
    }

    @TagListener(tag = ServerBattleTag.TAG_BATTLE_COMMON_FIGHT,messageClass = FightChoose.class)
    public StringMessage commonFight(ChannelHandlerContext ctx, FightChoose data) throws Exception {

        TownMap townMap = MapFactory.createMap(MapFactory.getMapById(data.getMapId()));

        UserGameWrapper gameWrapper = ctx.channel().attr(SessionAttributeKey.GAME).get();
        HeroWrapper currentHero = gameWrapper.getCurrentHero();
        Hero hero = currentHero.getHero();

        // 计算野怪的类型 数目 等级 品质
        List<Monster> monsters = townMap.getFindMonsterEngine().findMonsters(currentHero, townMap);
        BattleEngine engine = new BattleEngine(currentHero);
        BattleFightResult fight = engine.fight(BeanCopyUtils.deepCopy(Arrays.asList(hero)), monsters);
        fight.setMapId(data.getMapId());
        fight.setMapList(MapTemplateConvert.INSTANCE.toMapItems(MapFactory.mapTemplates()));

        // 获取地图列表
        StringMessage message = StringMessage.create(data.getSuccessCallbackTopic(), data.getSuccessCallbackTag());
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
        message.setBody(JSON.toJSONString(fight));
        return message;
    }
}
